package com.icloud.system.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icloud.common.database.mapper.AppCustomerMapper;
import com.icloud.common.database.model.AppCustomer;
import com.icloud.common.web.util.ObjectUtil;
import com.icloud.system.api.dto.SaveAppOperationLogDto;
import com.icloud.system.boot.mapper.AppWhiteListOperationLogMapper;
import com.icloud.system.boot.model.AppWhiteListOperationLog;
import com.icloud.system.boot.service.AppWhiteListOperationLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * App白名单操作日志service实现类
 */
@Service
public class AppWhiteListOperationLogServiceImpl extends ServiceImpl<AppWhiteListOperationLogMapper, AppWhiteListOperationLog> implements AppWhiteListOperationLogService {
    @Autowired
    private AppCustomerMapper appCustomerMapper;

    /**
     * 添加App白名单操作日志
     */
    @Override
    public void addAppWhiteListOperationLog(SaveAppOperationLogDto dto) {
        AppWhiteListOperationLog appOperationLog = new AppWhiteListOperationLog();
        BeanUtil.copyProperties(dto, appOperationLog, ObjectUtil.getNullField(dto));
        Long customerId = appOperationLog.getCustomerId();
        String username = appOperationLog.getUsername();
        if(customerId==null && StringUtils.isNotBlank(username)){
            LambdaQueryWrapper<AppCustomer> custQuery = new LambdaQueryWrapper<>();
            custQuery.eq(AppCustomer::getUsername, username);
            AppCustomer appCustomer = appCustomerMapper.selectOne(custQuery);
            customerId = appCustomer.getId();
            appOperationLog.setCustomerId(customerId);
        }
        save(appOperationLog);
    }
}
